package org.jeecg.modules.demo.document.service.impl;

import org.jeecg.modules.demo.document.mapper.FilesDao;
import org.jeecg.modules.demo.document.service.FilesService;
import org.jeecg.modules.demo.document.utils.FilesDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;





@Service
public class FilesServiceImpl implements FilesService {
	@Autowired
	private FilesDao filesDao;
	
	@Override
	public FilesDO get(Integer id){
		return filesDao.get(id);
	}
	
	@Override
	public List<FilesDO> list(Map<String, Object> map){
		return filesDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return filesDao.count(map);
	}
	
	@Override
	public int save(FilesDO file){
		return filesDao.save(file);
	}
	
	@Override
	public int update(FilesDO file){
		return filesDao.update(file);
	}
	
	@Override
	public int remove(Integer id){
		return filesDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return filesDao.batchRemove(ids);
	}

	@Override
	public File putBatchFilesInZip(List<FilesDO> files, File tempFile) throws IOException {
		ZipOutputStream zos = new ZipOutputStream(tempFile);
		for(FilesDO filesDO : files) {
			File inputFile = new File(filesDO.getFilepath());
			try(FileInputStream fis = new FileInputStream(inputFile);){
				//压缩文件中写入名称
				ZipEntry entry = new ZipEntry(filesDO.getFilename());
				zos.putNextEntry(entry);
				// 向压缩文件中输出数据
				int len = 0;
				byte[] bt = new byte[5*1024];
				while ((len = fis.read(bt)) != -1) {
					//压缩文件中写入真正的文件流
					zos.write(bt, 0, len);
				}

			}catch(Exception e) {
				e.printStackTrace();
				throw new RuntimeException();
			}

		}
		zos.flush();
		zos.close();
		return tempFile;
	}

	@Override
	public File putFilesPathInZip(List<String> filespath, File tempFile, List<String> names,List<Integer> idList) throws IOException{
		ZipOutputStream zos = new ZipOutputStream(tempFile);
		int i=0;
		for(String files : filespath) {
			File inputFile = new File(files);
			try(FileInputStream fis = new FileInputStream(inputFile)){
				//压缩文件中写入名称
//				ZipEntry entry = new ZipEntry(names.get(i)+"湖北省个人科技特派员申请表"+idList.get(i)+".docx");//压缩包中文件名称
				ZipEntry entry = new ZipEntry(names.get(i)+"湖北省科技特派员申请表"+idList.get(i)+".docx");//压缩包中文件名称
				zos.setEncoding("UTF-8");
				zos.putNextEntry(entry);
				// 向压缩文件中输出数据
				int len = 0;
				byte[] bt = new byte[5*1024];
				while ((len = fis.read(bt)) != -1) {
					//压缩文件中写入真正的文件流
					zos.write(bt, 0, len);
				}

			}catch(Exception e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
			i++;
		}
		zos.flush();
		zos.close();
		return tempFile;
	}

	@Override
	public List<FilesDO> getbyuserid(long userid) {
		return filesDao.getbyuserid(userid);
	}

	@Override
	public List<FilesDO> getbyNote1(String note1) {
		return filesDao.getbyNote1(note1);
	}

	@Override
	public List<FilesDO> getAll() {
		return filesDao.getAll();
	}
}

